/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { search, searchDetail } from "@/services/advanced";
import { message } from "antd";
import { isLegalData } from "@/utils/utils";

export default {
  namespace: "advanced",

  state: {
    result: null,
  },

  effects: {
    *search({ payload }, { call, put }) {
      const response = yield call(search, payload);
      if (!response) return;
      const { code, data = [], msg } = response;
      if (code > 0 || !data) {
        message.error(msg);
      }
      yield put({
        type: "save",
        payload: {
          result: code == 0 && data ? isLegalData(data) : null,
        },
      });
    },
    *searchDetail({ payload, index }, { call, put, select }) {
      const response = yield call(searchDetail, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { gp_record_list = [], data: waybill_records = [] } = data;
      if (code > 0 || !data) {
        message.error(msg);
      }
      const result = [...(yield select(_ => _.advanced.result))];
      result.splice(index, 1, {
        ...result[index],
        operates: isLegalData(waybill_records),
        gpRecords: isLegalData(gp_record_list),
      });
      yield put({
        type: "save",
        payload: {
          result,
        },
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
